package com.cqupt.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * 工具类
 */
public class APIUtils {
    //    url地址
    private static String URL = null;
    //    用户名
    private static String USERNAME = null;
    //    密码
    private static String PASSWORD = null;
    //    token
    private static String TOKEN = null;
    static {
        try {
            Properties properties = PropertiesLoaderUtils.loadAllProperties("zabbix.properties");
            URL = properties.getProperty("url");
            USERNAME = properties.getProperty("username");
            PASSWORD = properties.getProperty("password");
            updateToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新
     * token
     */
    public static void updateToken() {
        Map<String, Object> paramsMap = new HashMap<String, Object>(){{
            put("jsonrpc", "2.0");
            put("method", "user.login");
            put("id", 1);
            put("auth", null);
            put("params", new HashMap<String, Object>(){{
                put("user", USERNAME);
                put("password", PASSWORD);
            }});
        }};
        Map<String, Object> responseMap = APIUtils.sendRequest(JSON.toJSONString(paramsMap, SerializerFeature.WriteMapNullValue));
        TOKEN = responseMap.get("result").toString();
    }

    /**
     * 获取token
     * @return 返回token
     */
    public static String getToken() {
        return TOKEN;
    }

    /**
     * 发送请求
     * 以json字符串的形式
     * @param paramsJsonString
     * @return 返回一个map
     */

    public static Map<String, Object> sendRequest(String paramsJsonString) {
        Map<String, Object> resultMap = new HashMap<>();
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        try {
            httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(URL);
            httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
            httpPost.setEntity(new StringEntity(paramsJsonString, "utf-8"));
            httpResponse = httpClient.execute(httpPost);
            HttpEntity responseEntity = httpResponse.getEntity();
            String strResult = EntityUtils.toString(responseEntity, "utf-8");
            resultMap = JSONObject.parseObject(strResult, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            try {
                if (httpResponse != null) {
                    httpResponse.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultMap;
    }
}